package com.industrial.premu.service.impl.sqsj;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.regex.Pattern;

import cn.hutool.core.util.IdUtil;
import com.industrial.common.core.utils.DateUtils;
import com.industrial.common.core.utils.StringUtils;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.domain.DgQuantityQualityManage;
import com.industrial.premu.domain.SysOperError;
import com.industrial.premu.domain.sqsj.PreMuImportGuotu;
import com.industrial.premu.domain.sqsj.PreMuImportLog;
import com.industrial.premu.domain.sqsj.PreMuImportShuiwu;
import com.industrial.premu.mapper.DgQuantityQualityManageMapper;
import com.industrial.premu.mapper.SysOperErrorMapper;
import com.industrial.premu.mapper.sqsj.PreMuImportLogMapper;
import com.industrial.premu.mapper.sqsj.PreMuImportShuiwuMapper;
import com.industrial.premu.service.IPreMuImportLogService;
import com.industrial.premu.service.IPreMuImportShuiwuService;
import com.industrial.premu.util.ValidationUtil;
import com.industrial.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


/**
 * 亩产导入数据-税务局Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-25
 */
@Service
public class PreMuImportShuiwuServiceImpl implements IPreMuImportShuiwuService
{
    @Autowired
    private PreMuImportShuiwuMapper preMuImportShuiwuMapper;

    private static final int BATCH_SIZE = 300;

    @Autowired
    private DgQuantityQualityManageMapper dgQuantityQualityManageMapper;

    private static final String pattern = "^\\d+(\\.\\d+)?$";

    private static final String negSupportPattern = "^-?\\d+(\\.\\d+)?$";

    @Autowired
    private SysOperErrorMapper sysOperErrorMapper;

    /**
     * 查询亩产导入数据-税务局
     *
     * @param id 亩产导入数据-税务局主键
     * @return 亩产导入数据-税务局
     */
    @Override
    public PreMuImportShuiwu selectPreMuImportShuiwuById(String id)
    {
        return preMuImportShuiwuMapper.selectPreMuImportShuiwuById(id);
    }

    /**
     * 查询亩产导入数据-税务局列表
     *
     * @param preMuImportShuiwu 亩产导入数据-税务局
     * @return 亩产导入数据-税务局
     */
    @Override
    public List<PreMuImportShuiwu> selectPreMuImportShuiwuList(PreMuImportShuiwu preMuImportShuiwu)
    {
        return preMuImportShuiwuMapper.selectPreMuImportShuiwuList(preMuImportShuiwu);
    }

    /**
     * 新增亩产导入数据-税务局
     *
     * @param preMuImportShuiwu 亩产导入数据-税务局
     * @return 结果
     */
    @Override
    public int insertPreMuImportShuiwu(PreMuImportShuiwu preMuImportShuiwu)
    {
        return preMuImportShuiwuMapper.insertPreMuImportShuiwu(preMuImportShuiwu);
    }

    /**
     * 修改亩产导入数据-税务局
     *
     * @param preMuImportShuiwu 亩产导入数据-税务局
     * @return 结果
     */
    @Override
    public int updatePreMuImportShuiwu(PreMuImportShuiwu preMuImportShuiwu)
    {
        return preMuImportShuiwuMapper.updatePreMuImportShuiwu(preMuImportShuiwu);
    }

    /**
     * 批量删除亩产导入数据-税务局
     *
     * @param ids 需要删除的亩产导入数据-税务局主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportShuiwuByIds(String[] ids)
    {
        return preMuImportShuiwuMapper.deletePreMuImportShuiwuByIds(ids);
    }

    /**
     * 删除亩产导入数据-税务局信息
     *
     * @param id 亩产导入数据-税务局主键
     * @return 结果
     */
    @Override
    public int deletePreMuImportShuiwuById(String id)
    {
        return preMuImportShuiwuMapper.deletePreMuImportShuiwuById(id);
    }
    @Autowired
    private PreMuImportLogMapper preMuImportLogMapper;
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void removeData(PreMuImportShuiwu params,String deptCode) {
        preMuImportShuiwuMapper.removeData(params);

        PreMuImportLog removeParams = new PreMuImportLog();
        removeParams.setYear(params.getYear());
        removeParams.setCounty(SecurityUtils.getLoginUser().getSysUser().getDistrict());
        removeParams.setDeptCode(deptCode);
        preMuImportLogMapper.removeData(removeParams);
    }

    @Override
    public AjaxResult importShuiWuExcelData(String year, String companyType, MultipartFile file) {
        try{
            ExcelUtil<PreMuImportShuiwu> util = new ExcelUtil<PreMuImportShuiwu>(PreMuImportShuiwu.class);
            List<PreMuImportShuiwu> preMuImportShuiwus = util.importExcel(file.getInputStream());
            if (StringUtils.isNull(preMuImportShuiwus) || preMuImportShuiwus.size() == 0) {
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("allMsg","导入数据不能为空");
                return AjaxResult.error("导入失败",resultMap);
            }

            List<PreMuImportShuiwu> realList = new ArrayList<PreMuImportShuiwu>();
            int successNum = 0;
            int failureNum = 0;
            int updateNum = 0;
            SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
            //企业名称为空的错误数据量
            int companyNameBlankCount = 0;
            //值为空的错误量
            int valueErrorCount = 0;
            int valueZeroCount = 0;

            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            StringBuilder allMsg = new StringBuilder();

            //批处理用
            List<String> usccList = new ArrayList<String>();
            List<String> mcImportExistList = new ArrayList<String>();

            List<PreMuImportShuiwu> addImportList = new ArrayList<PreMuImportShuiwu>();
            List<PreMuImportShuiwu> updateImportList = new ArrayList<PreMuImportShuiwu>();

            if (preMuImportShuiwus != null && !preMuImportShuiwus.isEmpty()) {


                for(PreMuImportShuiwu data : preMuImportShuiwus){

                    if("2".equals(companyType)){
                        data.setCompanyType("2");
                    }

                    if (data.getUscc() != null && !"".equals(data.getUscc())) {

                        usccList.add(data.getUscc().trim());
                        realList.add(data);
                    }
                }

//                preMuImportShuiwus.forEach(data -> {
//                    if (data.getUscc() != null && !"".equals(data.getUscc())) {
//
//                        usccList.add(data.getUscc().trim());
//                        realList.add(data);
//                    }
//
//                });
            }

            long count = usccList.stream().distinct().count();
            boolean isRepeat = count < usccList.size();
            if (isRepeat) {
                Map<String,String> resultMap = new HashMap<>();
                resultMap.put("allMsg","有重复统一社会信用代码，请检查导入数据！");
                return AjaxResult.error("导入失败",resultMap);
            }
            mcImportExistList = preMuImportShuiwuMapper.getImportShuiwuExistList(year, usccList);
            ValidationUtil.validate(realList);

            int travel = 1;
            for (int i = 0; i < preMuImportShuiwus.size(); i++) {
                travel++;
                PreMuImportShuiwu mcImportShuiwuDO = preMuImportShuiwus.get(i);
                mcImportShuiwuDO.setYear(year);

                String uscc = mcImportShuiwuDO.getUscc().trim();
                if (uscc == null || "".equals(uscc) || year == null || "".equals(year)) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
                    failureMsg.append(msg + "社会信用代码或年份不能为空");
                    continue;
                } else {
                    uscc = uscc.trim();
                    mcImportShuiwuDO.setUscc(uscc);
                }

                if(StringUtils.isBlank(mcImportShuiwuDO.getProfitTotal()) || !Pattern.matches(negSupportPattern, mcImportShuiwuDO.getProfitTotal())){
                    valueErrorCount++;
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
                    failureMsg.append(msg + "利润总额不可为空或不是数字类型");
                    continue;
                }



                if(StringUtils.isBlank(mcImportShuiwuDO.getCompanyName())){
                    companyNameBlankCount++;
                    continue;
                }

                if("1".equals(companyType)){
                    if(StringUtils.isBlank(mcImportShuiwuDO.getMainIncome()) || !Pattern.matches(pattern, mcImportShuiwuDO.getMainIncome())){
                        valueErrorCount++;
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "营业收入收不可为空或不是数字类型");
                        continue;
                    }

//                    if(StringUtils.isBlank(mcImportShuiwuDO.getActualTax()) || StringUtils.isBlank(mcImportShuiwuDO.getRdFunds())){
//                        failureNum++;
//                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
//                        failureMsg.append(msg + "研发经费、税收不可为空");
//                        continue;
//                    }
                }else{
                    if(StringUtils.isBlank(mcImportShuiwuDO.getMainIncome()) || !Pattern.matches(pattern, mcImportShuiwuDO.getMainIncome())){
                        valueErrorCount++;
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "营业收入不可为空或不是数字类型");
                        continue;
                    }

//                    if(StringUtils.isBlank(mcImportShuiwuDO.getActualTax())){
//                        failureNum++;
//                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
//                        failureMsg.append(msg + "税收不可为空");
//                        continue;
//                    }
                }



                BigDecimal mainIncomeBig = StringUtils.isBlank(mcImportShuiwuDO.getMainIncome())? BigDecimal.ZERO : new BigDecimal(mcImportShuiwuDO.getMainIncome());
                BigDecimal profitTotalBig = StringUtils.isBlank(mcImportShuiwuDO.getProfitTotal())? BigDecimal.ZERO : new BigDecimal(mcImportShuiwuDO.getProfitTotal());
//                BigDecimal rdFundsBig = StringUtils.isBlank(mcImportShuiwuDO.getRdFunds())? BigDecimal.ZERO : new BigDecimal(mcImportShuiwuDO.getRdFunds());
//                BigDecimal actualTaxBig = StringUtils.isBlank(mcImportShuiwuDO.getActualTax())? BigDecimal.ZERO : new BigDecimal(mcImportShuiwuDO.getActualTax());

                if(mainIncomeBig.compareTo(BigDecimal.ZERO) < 1){
                        valueZeroCount++;
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
                        failureMsg.append(msg + "营业收入必须大于0");
                        continue;
                }
//                if("1".equals(companyType)){
//                    if(mainIncomeBig.compareTo(BigDecimal.ZERO) < 1 || rdFundsBig.compareTo(BigDecimal.ZERO) < 1 || actualTaxBig.compareTo(BigDecimal.ZERO) < 1){
//                        valueZeroCount++;
//                        failureNum++;
//                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
//                        failureMsg.append(msg + "销售收入不可小于等于0");
//                        continue;
//                    }
//                }else{
//                    if(mainIncomeBig.compareTo(BigDecimal.ZERO) < 1 || actualTaxBig.compareTo(BigDecimal.ZERO) < 1){
//                        valueZeroCount++;
//                        failureNum++;
//                        String msg = "<br/>" + failureNum + "、企业 " + mcImportShuiwuDO.getCompanyName() + " 导入失败：";
//                        failureMsg.append(msg + "销售收入不可小于等于0");
//                        continue;
//                    }
//                }


                BigDecimal zeroBig = new BigDecimal("0.0000");
                if (mcImportShuiwuDO.getActualTax() == null) {
                    mcImportShuiwuDO.setActualTax(zeroBig.toString());
                }
                BigDecimal bigDecimal = new BigDecimal(mcImportShuiwuDO.getActualTax());

                if (bigDecimal.compareTo(zeroBig) < 0) {
                    mcImportShuiwuDO.setActualTax(zeroBig.toString());
                }

                if (mcImportShuiwuDO.getMainIncome() == null) {
                    mcImportShuiwuDO.setMainIncome(zeroBig.toString());
                }
                if (mainIncomeBig.compareTo(zeroBig) < 0) {
                    mcImportShuiwuDO.setMainIncome(zeroBig.toString());
                }

                if(StringUtils.isNotBlank(mcImportShuiwuDO.getMainIncome())){
                    //保留两位小数
                    BigDecimal mainIncomeNumeric = new BigDecimal(mcImportShuiwuDO.getMainIncome()).setScale(2, RoundingMode.HALF_UP);
                    mcImportShuiwuDO.setMainIncome(mainIncomeNumeric.toString());
                }
                if(StringUtils.isNotBlank(mcImportShuiwuDO.getProfitTotal())){
                    //保留两位小数
                    BigDecimal profitTotalNumeric = new BigDecimal(mcImportShuiwuDO.getProfitTotal()).setScale(2, RoundingMode.HALF_UP);
                    mcImportShuiwuDO.setProfitTotal(profitTotalNumeric.toString());
                }

                if(StringUtils.isNotBlank(mcImportShuiwuDO.getActualTax())){
                    //保留两位小数
                    BigDecimal actualTaxNumeric = new BigDecimal(mcImportShuiwuDO.getActualTax()).setScale(2, RoundingMode.HALF_UP);
                    mcImportShuiwuDO.setActualTax(actualTaxNumeric.toString());
                }

                if (mcImportExistList.contains(uscc)) {
                    mcImportShuiwuDO.setUpdateDate(DateUtils.getNowDate());
                    mcImportShuiwuDO.setUpdateBy(SecurityUtils.getUserId());
                    mcImportShuiwuDO.setDelFlag("0");
                    updateImportList.add(mcImportShuiwuDO);
                } else {
                    mcImportShuiwuDO.setId(UUID.randomUUID().toString().replace("-", ""));
                    mcImportShuiwuDO.setDelFlag("0");
                    mcImportShuiwuDO.setCreateDate(DateUtils.getNowDate());
                    mcImportShuiwuDO.setCreateBy(SecurityUtils.getUserId());
                    mcImportShuiwuDO.setUpdateDate(DateUtils.getNowDate());
                    mcImportShuiwuDO.setUpdateBy(SecurityUtils.getUserId());
                    addImportList.add(mcImportShuiwuDO);
                }

                if (addImportList != null && addImportList.size() >= BATCH_SIZE) {

                    int importNum = preMuImportShuiwuMapper.batchSave(addImportList);
                    successNum += importNum;
                    addImportList.clear();
                }
                if (updateImportList != null && updateImportList.size() >= BATCH_SIZE) {

                    int importNum = preMuImportShuiwuMapper.batchUpdateByUscc(year, updateImportList);
                    successNum += importNum;
                    updateNum += importNum;
                    updateImportList.clear();
                }
            }

            if (addImportList != null && !addImportList.isEmpty()) {
                int importNum = preMuImportShuiwuMapper.batchSave(addImportList);
                successNum += importNum;
                addImportList.clear();
            }
            if (updateImportList != null && !updateImportList.isEmpty()) {
                int importNum = preMuImportShuiwuMapper.batchUpdateByUscc(year, updateImportList);
                successNum += importNum;
                updateNum += importNum;
                updateImportList.clear();
            }

            if (failureNum > 0) {
                allMsg.insert(0, "<br/>很抱歉，错误如下：" + failureMsg.toString());
            }
            if (successNum > 0) {
                allMsg.insert(0, "恭喜您，共" + successNum + " 条数据导入成功！");
            }

            //企业名称为空的数量
            if(companyNameBlankCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("企业名称不能为空");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(companyNameBlankCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());

                    comapnNameBlankParams.setFailCount(String.valueOf(companyNameBlankCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("企业名称不能为空");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(companyNameBlankCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }

            if(valueZeroCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值小于0的错误");

                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(valueZeroCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setFailCount(String.valueOf(valueZeroCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值小于0的错误");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(valueZeroCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }

            //值存在错误的情况
            if(valueErrorCount > 0){
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值不存在或者是格式有误");

                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());

                    String failCount = dgQuantityQualityManage.getFailCount();
                    Integer oldFailCount = Integer.parseInt(failCount);

                    dgQuantityQualityManage.setFailCount(String.valueOf(valueErrorCount + oldFailCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }else{
                    comapnNameBlankParams.setBaseId(IdUtil.simpleUUID());
                    comapnNameBlankParams.setBaseCreateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseCreateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setBaseUpdateTime(DateUtils.getNowDate());
                    comapnNameBlankParams.setBaseUpdateBy(SecurityUtils.getUserId());
                    comapnNameBlankParams.setFailCount(String.valueOf(valueErrorCount));
                    comapnNameBlankParams.setRuleType("1");
                    dgQuantityQualityManageMapper.insertDgQuantityQualityManage(comapnNameBlankParams);
                }
            }else{
                DgQuantityQualityManage comapnNameBlankParams = new DgQuantityQualityManage();
                comapnNameBlankParams.setSource("税务局");
                comapnNameBlankParams.setYears(year);
                comapnNameBlankParams.setProvince(sysUser.getProvince());
                comapnNameBlankParams.setCity(sysUser.getCity());
                comapnNameBlankParams.setCounty(sysUser.getDistrict());
                comapnNameBlankParams.setRuleName("值不存在或者是格式有误");
                List<DgQuantityQualityManage> dgQuantityQualityManages = dgQuantityQualityManageMapper.selectDgQuantityQualityManageList(comapnNameBlankParams);
                if(dgQuantityQualityManages != null && !dgQuantityQualityManages.isEmpty()){
                    DgQuantityQualityManage dgQuantityQualityManage = dgQuantityQualityManages.get(0);
                    dgQuantityQualityManage.setBaseUpdateTime(DateUtils.getNowDate());
                    dgQuantityQualityManage.setBaseUpdateBy(SecurityUtils.getUserId());
                    dgQuantityQualityManage.setFailCount(String.valueOf(valueErrorCount));
                    dgQuantityQualityManageMapper.updateDgQuantityQualityManage(dgQuantityQualityManage);
                }
            }

            Map<String,String> resultMap = new HashMap<>();
            resultMap.put("msg","恭喜您，共" + successNum + " 条数据导入成功！");
            resultMap.put("sum",String.valueOf(successNum));
            resultMap.put("updateNum",String.valueOf(updateNum));
            resultMap.put("failureNum",String.valueOf(failureNum));
            resultMap.put("allMsg",allMsg.toString());
            return AjaxResult.success(resultMap);
        }catch (Exception e){
            String requestURI = "/industrial-premu/importExcelData";
            SysOperError sysOperError = new SysOperError();
            sysOperError.setBaseId(IdUtil.simpleUUID());
            sysOperError.setBaseCreateTime(new Date());
            sysOperError.setErrorMsg(e.getMessage());
            sysOperError.setUrlAddress(requestURI);
            sysOperError.setOperId(SecurityUtils.getUserId());
            sysOperErrorMapper.saveErrorInfo(sysOperError);
            e.printStackTrace();
            return AjaxResult.warn("导入失败");
        }

    }
}